
package org.come.Jpanel;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.PetOperationPanelBtn;
import com.tool.btn.goodbtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.Frame.ZhuFrame;
import org.come.entity.RoleSummoning;
import org.come.mouslisten.ChoseAlchemyGoodsMouslisten;
import org.come.mouslisten.PetAddPointMouslisten;
import org.come.until.*;



public class AlchemyJpanel extends JPanel {
    private JList<String> listpet;
    private DefaultListModel<String> listModel;
    private Color fontcolor;
    private JScrollPane jScrollPane;
    private PetOperationPanelBtn btnalchemy;
    private JLabel labRefined;
    private JLabel[] GoodsListLabel = new JLabel[24];
    private goodbtn[] btnrights;
    private final ChoseAlchemyGoodsMouslisten[] GoodsListListen = new ChoseAlchemyGoodsMouslisten[24];
    private int Flag = 0;
    private int count = 1;
    private ImageIcon icon;
    private RichLabel richLabel;
    private RichLabel namen;
    // 滚动条
    private JLabel labgundong;
    public AlchemyJpanel() throws Exception {
        labgundong = new JLabel(new ImageIcon("skin/8100226/button/23.png"));
        this.labgundong.setBounds(197, 288, 0, 0);
        this.add(this.labgundong);
        this.setPreferredSize(new Dimension(559, 471));
        this.setLayout(null);
        this.setOpaque(false);
        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 17);
        offBtn.setBounds(559 - 37, 10, 25, 25);
        this.add(offBtn);
        // 添加组件
        // 宠物列表对象
        listModel = new DefaultListModel<String>();
        // labgundong.setBounds(123, 268, 17, 152);
        // this.add(labgundong);

        (this.richLabel = new RichLabel("", UIUtils.TEXT_FONT2, 255)).setBounds(380, 65, this.richLabel.getWidth(), this.richLabel.getHeight());
        this.add((Component)this.richLabel);
        (this.namen = new RichLabel("", UIUtils.TEXT_NAME_FONT, 255)).setBounds(240, 77, this.richLabel.getWidth(), this.richLabel.getHeight());
        this.add((Component)this.namen);
        // 宠物列表

        listpet = new JList<String>();
        listpet.setOpaque(false);
        listpet.setSelectionBackground(new Color(33, 42, 52));
        fontcolor = Color.GREEN;
        listpet.setSelectionForeground(fontcolor);
        listpet.setForeground(fontcolor);
        listpet.setFont(new Font("微软雅黑", 0, 14));
        listpet.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
        listpet.setOpaque(false);
        listpet.setModel(listModel);
        listpet.addMouseListener(new MouseListener() {
            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                if (AlchemyJpanel.this.listpet.getSelectedIndex() != -1) {
                    UserMessUntil.setChosePetMes((RoleSummoning)UserMessUntil.getPetListTable().get(AlchemyJpanel.this.listpet.getSelectedIndex()));
                    PetAddPointMouslisten.showPetValue();
                    if (UserMessUntil.getChosePetMes() != null) {
                        final StringBuffer buffer = new StringBuffer();
                        buffer.append("#r#c8EDEFF炼妖次数:#R");
                        buffer.append(UserMessUntil.getChosePetMes().getAlchemynum());
                        buffer.append("/#R12#c8EDEFF");
                        String[] tss = null;
                        String[] lys = null;
                        if (UserMessUntil.getChosePetMes().getResistance() != null && !"".equals(UserMessUntil.getChosePetMes().getResistance())) {
                            tss = UserMessUntil.getChosePetMes().getResistance().split("\\|");
                        }
                        if (UserMessUntil.getChosePetMes().getLyk() != null && !"".equals(UserMessUntil.getChosePetMes().getLyk())) {
                            lys = UserMessUntil.getChosePetMes().getLyk().split("\\|");
                        }
                        if (tss != null) {
                            double t = 0.0;
                            double l = 0.0;
                            int i = 0;
                            while (i < tss.length) {
                                final String[] v = tss[i].split("=");
                                t = Double.parseDouble(v[1]);
                                final String qz = v[0] + "=";
                                l = 0.0;
                                if (lys != null) {
                                    int j = 0;
                                    while (j < lys.length) {
                                        if (lys[j] != null && lys[j].startsWith(qz)) {
                                            l = Double.parseDouble(lys[j].split("=")[1]);
                                            l = ((l < 45.0) ? l : 45.0);
                                            lys[j] = null;
                                            break;
                                        }
                                        else {
                                            if (lys[j] != null && qz.startsWith("抗物理") && lys[j].startsWith("物理吸收")) {
                                                l = Double.parseDouble(lys[j].split("=")[1]);
                                                l = ((l < 45.0) ? l : 45.0);
                                                lys[j] = null;
                                                break;
                                            }
                                            else {
                                                ++j;
                                            }
                                        }
                                    }
                                }
                                buffer.append("#r");
                                buffer.append(v[0]);
                                buffer.append(": ");
                                buffer.append("#r");
                                buffer.append(t + l);
                                buffer.append("(#W");
                                buffer.append(t);
                                buffer.append("#c8EDEFF+");
                                buffer.append(l);
                                buffer.append(")#c8EDEFF");
                                buffer.append("#c00FFFF[");
                                buffer.append("75");
                                buffer.append("]#c8EDEFF");
                                ++i;
                            }
                        }
                        if (lys != null) {
                            int size = 0;
                            int k = 0;
                            while (k < lys.length) {
                                if (lys[k] != null) {
                                    buffer.append((size % 2 == 0) ? "#r" : "");
                                    final String[] kx = lys[k].split("=");
                                    buffer.append(kx[0]);
                                    buffer.append(": ");
                                    buffer.append((Double.valueOf(kx[1]).doubleValue() > 75.0) ? 75.0 : Double.valueOf(kx[1]).doubleValue());
                                    buffer.append("#r");
                                    ++size;
                                }
                                ++k;
                            }
                        }
                        AlchemyJpanel.this.richLabel.setTextSize(buffer.toString(), 255);
                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {

                ZhuFrame.getZhuJpanel().pettext();
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                if (UserMessUntil.getChosePetMes() != null) {
                    ZhuFrame.getZhuJpanel().creatpettext(UserMessUntil.getChosePetMes());
                }
            }

            @Override
            public void mouseClicked(MouseEvent e) {

                // 打开了窗体
                if (e.isMetaDown()) {// 检测鼠标右键单击
                    FormsManagement.HideForm(17);
                }

            }
        });
        if (MyIsif.getStyle().equals("水墨UI")){
            // 宠物列表滚动条
            jScrollPane = new JScrollPane(listpet);
            jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
            jScrollPane.getVerticalScrollBar().setUnitIncrement(30);
            jScrollPane.getViewport().setOpaque(false);
            jScrollPane.setOpaque(false);
            jScrollPane.setBounds(23 + 21, 250 + 19, 117 + 54, 185);
            // jScrollPane.setBorder(BorderFactory.createLineBorder(new Color(31, 7,
            // 8)));
            jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            this.add(jScrollPane);

            // 放置炼化石
            labRefined = new JLabel();
            labRefined.setBounds(260, 93, 50, 51);
            labRefined.addMouseListener(new ChoseAlchemyGoodsMouslisten(24));
            this.add(labRefined);

            // 炼妖按钮
            btnalchemy = new PetOperationPanelBtn("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16,"炼妖");
            btnalchemy.setBounds(202 + 54, 160, 59, 24);
            this.add(btnalchemy);

            // 存放物品的表格1~24
            for (int i = 0; i < 24; i++) {
                GoodsListLabel[i] = new JLabel();
                GoodsListListen[i] = new ChoseAlchemyGoodsMouslisten(i);
                GoodsListLabel[i].addMouseListener(GoodsListListen[i]);
                if ((Flag < 6) && (count == 1)) {
                    GoodsListLabel[i].setBounds(215 + Flag * 50, 250, 49, 49);
                    Flag++;// 用来标记行s
                    this.add(GoodsListLabel[i]);
                }
                if ((Flag < 6) && (count == 2)) {
                    GoodsListLabel[i].setBounds(215 + Flag * 50, 299, 49, 49);
                    Flag++;// 用来标记行
                    this.add(GoodsListLabel[i]);
                }
                if ((Flag < 6) && (count == 3)) {
                    GoodsListLabel[i].setBounds(215 + Flag * 50, 348, 49, 49);
                    Flag++;// 用来标记行
                    this.add(GoodsListLabel[i]);
                }
                if ((Flag < 6) && (count == 4)) {
                    GoodsListLabel[i].setBounds(215 + Flag * 50, 397, 49, 49);
                    Flag++;// 用来标记行
                    this.add(GoodsListLabel[i]);

                } else if (Flag == 6) {
                    Flag = 0;
                    count++;
                }
            }

            // 背包四个页数的按钮
            btnrights = new goodbtn[6];
            for (int i = 0; i < btnrights.length; i++) {
                btnrights[i] = new goodbtn("skin/8100226/newbutton/C0"+(i+1)+".png", 0, this, i);
                btnrights[i].setBounds(526, 248 + i * 35, 35, 31);
                this.add(btnrights[i]);
            }
        }else {
            // 宠物列表滚动条
            jScrollPane = new JScrollPane(listpet);
            jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            jScrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
            jScrollPane.getVerticalScrollBar().setUnitIncrement(30);
            jScrollPane.getViewport().setOpaque(false);
            jScrollPane.setOpaque(false);
            jScrollPane.setBounds(23 + 21, 250 + 19, 117 + 54, 185);
            // jScrollPane.setBorder(BorderFactory.createLineBorder(new Color(31, 7,
            // 8)));
            jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            this.add(jScrollPane);

            // 放置炼化石
            labRefined = new JLabel();
            labRefined.setBounds(262, 83, 50, 51);
            labRefined.addMouseListener(new ChoseAlchemyGoodsMouslisten(24));
            this.add(labRefined);

            // 炼妖按钮
            btnalchemy = new PetOperationPanelBtn("inkImg/hongmu/1_png.button.btn_xy.png", 1, UIUtils.COLOR_WHITE3, UIUtils.TEXT_HY16,"炼妖");
            btnalchemy.setBounds(200 + 50, 160, 76, 26);
            this.add(btnalchemy);

            // 存放物品的表格1~24
            for (int i = 0; i < 24; i++) {
                GoodsListLabel[i] = new JLabel();
                GoodsListListen[i] = new ChoseAlchemyGoodsMouslisten(i);
                GoodsListLabel[i].addMouseListener(GoodsListListen[i]);
                if ((Flag < 6) && (count == 1)) {
                    GoodsListLabel[i].setBounds(215 + Flag * 50, 250, 49, 49);
                    Flag++;// 用来标记行s
                    this.add(GoodsListLabel[i]);
                }
                if ((Flag < 6) && (count == 2)) {
                    GoodsListLabel[i].setBounds(215 + Flag * 50, 299, 49, 49);
                    Flag++;// 用来标记行
                    this.add(GoodsListLabel[i]);
                }
                if ((Flag < 6) && (count == 3)) {
                    GoodsListLabel[i].setBounds(215 + Flag * 50, 348, 49, 49);
                    Flag++;// 用来标记行
                    this.add(GoodsListLabel[i]);
                }
                if ((Flag < 6) && (count == 4)) {
                    GoodsListLabel[i].setBounds(215 + Flag * 50, 397, 49, 49);
                    Flag++;// 用来标记行
                    this.add(GoodsListLabel[i]);

                } else if (Flag == 6) {
                    Flag = 0;
                    count++;
                }
            }

            // 背包四个页数的按钮
            btnrights = new goodbtn[6];
            for (int i = 0; i < btnrights.length; i++) {
//                btnrights[i] = new goodbtn("skin/8100226/newbutton/C0"+(i+1)+".png", 0, this, i);
                btnrights[i] = new goodbtn("img/xy2uiimg/44_png.button.tab_pak.png", 0, this, i);
                btnrights[i].setBounds(526, 248 + i * 35, 35, 31);
                this.add(btnrights[i]);
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        if (MyIsif.getStyle().equals("水墨UI")){
            super.paintComponent(g);
            if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B256.png");
            }
            // 炼妖
            g.drawImage(icon.getImage(), 0, 0, 559, 471, this);
            if (UserMessUntil.getChosePetMes() != null) {
                if (TestPetJpanel.part==null) {
                    TestPetJpanel.part=UserMessUntil.getChosePetMes().getPart();
                }
                TestPetJpanel.part.draw(g, 120, 190, 0, ImageMixDeal.userimg.getTime());
            }
        }else {
            super.paintComponent(g);
            if (icon == null) {
                icon = new ImageIcon("skin/8100226/button/hongmu/炼妖红木.png");
            }
            // 炼妖
            g.drawImage(icon.getImage(), 0, 0, 559, 471, this);
            if (UserMessUntil.getChosePetMes() != null) {
                if (TestPetJpanel.part==null) {
                    TestPetJpanel.part=UserMessUntil.getChosePetMes().getPart();
                }
                TestPetJpanel.part.draw(g, 120, 190, 0, ImageMixDeal.userimg.getTime());
            }
        }
        // 画物品的图片和数量
        GoodsListFromServerUntil.draw(g, 223, 250);
        if (this.listModel.size() > 8) {
            this.remove(this.labgundong);
        }
    }

    public RichLabel getRichLabel() {
        return this.richLabel;
    }

    public void setRichLabel(final RichLabel richLabel) {
        this.richLabel = richLabel;
    }

    public RichLabel getNamen() {
        return this.namen;
    }

    public void setNamen(final RichLabel namen) {
        this.namen = namen;
    }

    public JList<String> getListpet() {
        return this.listpet;
    }

    public void setListpet(JList<String> listpet) {
        this.listpet = listpet;
    }

    public DefaultListModel<String> getListModel() {
        return this.listModel;
    }

    public void setListModel(DefaultListModel<String> listModel) {
        this.listModel = listModel;
    }

    public Color getFontcolor() {
        return this.fontcolor;
    }

    public void setFontcolor(Color fontcolor) {
        this.fontcolor = fontcolor;
    }

    public JScrollPane getjScrollPane() {
        return this.jScrollPane;
    }

    public void setjScrollPane(JScrollPane jScrollPane) {
        this.jScrollPane = jScrollPane;
    }

    public PetOperationPanelBtn getBtnalchemy() {
        return this.btnalchemy;
    }

    public void setBtnalchemy(PetOperationPanelBtn btnalchemy) {
        this.btnalchemy = btnalchemy;
    }

    public JLabel getLabRefined() {
        return this.labRefined;
    }

    public void setLabRefined(JLabel labRefined) {
        this.labRefined = labRefined;
    }

    public JLabel[] getGoodsListLabel() {
        return this.GoodsListLabel;
    }

    public void setGoodsListLabel(JLabel[] goodsListLabel) {
        this.GoodsListLabel = goodsListLabel;
    }

    public int getFlag() {
        return this.Flag;
    }

    public void setFlag(int flag) {
        this.Flag = flag;
    }

    public int getCount() {
        return this.count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public goodbtn[] getBtnrights() {
        return this.btnrights;
    }

    public void setBtnrights(goodbtn[] btnrights) {
        this.btnrights = btnrights;
    }
}
